package com.qinggeng.project.api.controller;

import com.qinggeng.common.constant.Constants;
import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.security.annotation.LoginRequired;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.api.utils.PhoneNumberUtils;
import com.qinggeng.project.system.domain.RegionalAgency;
import com.qinggeng.project.system.domain.Supplier;
import com.qinggeng.project.system.domain.SysLeague;
import com.qinggeng.project.system.service.IRegionalAgencyService;
import com.qinggeng.project.system.service.ISupplierService;
import com.qinggeng.project.system.service.ISysLeagueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "加盟模块")
@RestController
@RequestMapping("${apiPath}/join")
@CrossOrigin
public class ApiJoinController extends BaseController {
    @Autowired
    private ISysLeagueService leagueService;
    @Autowired
    private IRegionalAgencyService agencyService;
    @Autowired
    private ISupplierService supplierService;

    private static final int lengthLimit=100;



    @ApiOperation("门店入驻")
    @PostMapping("/point")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记")
    })
    @Log(title = "门店入驻", businessType = BusinessType.INSERT)
    @LoginRequired(must = false,role = ApiUtils.DEFAULT_ROLE_USER)
    public AjaxResult leagueAdd(SysLeague league) {
        checkLength("姓名",league.getLeagueName());
        if(!PhoneNumberUtils.isMobile(league.getLeaguePhone())){
            return AjaxResult.error("手机号码格式不正确",ShowTypeEnum.用户.getValue());
        }
        /*if(!league.getLeagueEmail().matches("^\\w+@(\\w+\\.)+\\w+$")){
            return AjaxResult.error("邮箱格式不正确",ShowTypeEnum.用户.getValue());
        }*/
        checkLength("所在区域",league.getLeagueArea());
        return toAjax(leagueService.insertSysLeague(league));
    }

    @ApiOperation("成为代理")
    @PostMapping("/agency")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记")
    })
    @Log(title = "成为代理", businessType = BusinessType.INSERT)
    @LoginRequired(must = false,role = ApiUtils.DEFAULT_ROLE_USER)
    public AjaxResult agencyAdd(RegionalAgency agency) {
        checkLength("姓名",agency.getName());
        if(!PhoneNumberUtils.isMobile(agency.getPhone())){
            return AjaxResult.error("手机号码格式不正确",ShowTypeEnum.用户.getValue());
        }
        checkLength("年龄",agency.getAge());
        checkLength("代理区域",agency.getArea());
        checkLength("加入理由",agency.getReason());
        return toAjax(agencyService.insertRegionalAgency(agency));
    }

    @ApiOperation("我要供货")
    @PostMapping("/supplier")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记")
    })
    @Log(title = "我要供货", businessType = BusinessType.INSERT)
    @LoginRequired(must = false,role = ApiUtils.DEFAULT_ROLE_USER)
    public AjaxResult supplierAdd(Supplier supplier) {
        checkLength("姓名",supplier.getName());
        if(!PhoneNumberUtils.isMobile(supplier.getPhone())){
            return AjaxResult.error("手机号码格式不正确",ShowTypeEnum.用户.getValue());
        }
        checkLength("供货类别",supplier.getCategory());
        checkLength("产品规格",supplier.getSpec());
        checkLength("价格",supplier.getPrice());
        return toAjax(supplierService.insertSupplier(supplier));
    }

    private void checkLength(String name,String value){
        /*if(StringUtils.isEmpty(value)){
            throw new CustomException(name+"不能为空",ShowTypeEnum.用户.getValue());
        }*/
        if(value.length()>lengthLimit){
            throw new CustomException(name+"的长度不能超过"+lengthLimit+"字。", ShowTypeEnum.用户.getValue());
        }
    }



}
